/**************************************************************************
 * @name: start.c
 * @brief: 用于启动开机动画
 * @date: 2025/01/15
 * @author: boluzs@163.com
 * @note: None
 * @version: 1.0.0
 * Copyright (c) 2025 by boluzs@163.com, All Rights Reserved.
 **************************************************************************/
#include "main.h"
#include "start.h"
#include "bmp.h"
#include "menu.h"

int Start_Show(const char *Dir_Path, unsigned int x_off, unsigned int y_off)
{
    // 1.需要打开的目录
    DIR *dir_p = opendir(Dir_Path);
    // 2.切换工作目录
    int dir_d = chdir(Dir_Path);
    // 3.读取目录文件中的目录项
    struct dirent *dirent_p = NULL;

    char **file_names = (char **)calloc(1, 512 * 8);
    unsigned int cnt = 0;
    // 4.排序BMP图片并展示
    Bmp_Sort(dir_p, dirent_p, file_names, &cnt);
    for (int i = 0; i < cnt; i++)
    {
        Bmp_Show(file_names[i], lcd_buf, x_off, y_off);
        if (x_off == 0 || y_off == 0)
        {
            // 5.刷新屏幕
            Lcd_Refresh(lcd_mp, lcd_buf);
        }
    }
    // 关闭文件和映射
    free(file_names);
    closedir(dir_p);
    return 0;
}

/**************************************************************************
 * @name: Start_Time
 * @brief: 获取当前时间，并存储到Date_Str中
 * @param {char} *Date_Str
 * @note: None
 * @retval: None
 **************************************************************************/
void Start_Time(char *Date_Str)
{
    time_t timer = time(NULL);
    // 2.获取系统时间并转化为格式化字符串
    struct tm *Date = localtime(&timer);
    sprintf(Date_Str, "%.2d-%.2d-%.2d %.2d:%.2d:%.2d",
            Date->tm_year + 1900,
            Date->tm_mon + 1,
            Date->tm_mday,
            Date->tm_hour,
            Date->tm_min,
            Date->tm_sec);
}

/**************************************************************************
 * @name: Get_Time
 * @brief: 循环获取当前时间
 * @note: None
 * @retval: None
 **************************************************************************/
void Get_Time(void)
{
    // 无限循环尽量使用for()实现，原因是效率更快
    for (;;)
    {
        Start_Time(cur_datetime);
    }
}
